<?php
namespace App\Models;

/**
 * 钱包类
 */
class MWallet extends BaseModel
{
    public $table = "wallets";

    public static $WALLET_TYPE_1 = 1;   //奖金币标识
    public static $WALLET_TYPE_2 = 2;   //报单币标识
    public static $WALLET_TYPE_3 = 3;   //购货币标识
    public static $WALLET_TYPE_4 = 4;   //......
    public static $WALLET_TYPE_5 = 5;   //......

    public static $PAY_TYPE_ZFB = 1;    //支付宝
    public static $PAY_TYPE_WX = 2;     //微信
    public static $PAY_TYPE_YHK = 3;    //银行卡


    /**
     * 所属会员 1:1
     */
    public function user()
    {
        return $this->hasOne(MUser::class, 'id', 'uid');
    }


    /**
     * 钱包充值函数
     * $wallet_type = 钱包类型
     * $money = 充值金额
     */
    public function recharge($wallet_type, $money)
    {
        if ($wallet_type == self::$WALLET_TYPE_1) {  //奖金币充值
            $this->w1 += $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_2) {  //报单币充值
            $this->w2 += $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_3) {  //购物币充值
            $this->w3 += $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_4) {
            $this->w4 += $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_5) {
            $this->w5 += $money;
        }

        return $this->save();
    }

    /**
     * 升级会员
     * $wallet_type = 钱包类型
     * $money = 充值金额
     */
    public function upVip($wallet_type, $money)
    {
        if ($wallet_type == self::$WALLET_TYPE_1) {  //奖金币充值
            $this->w1 -= $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_2) {  //报单币充值
            $this->w2 -= $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_3) {  //购物币充值
            $this->w3 -= $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_4) {
            $this->w4 -= $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_5) {
            $this->w5 -= $money;
        }

        return $this->save();
    }


    /**
     * 钱包扣款函数
     * $wallet_type = 钱包类型
     * $money = 充值金额
     */
    public function deducting($wallet_type, $money)
    {
        if ($wallet_type == self::$WALLET_TYPE_1) {  //奖金币充值
            $this->w1 -= $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_2) {  //报单币充值
            $this->w2 -= $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_3) {  //购物币充值
            $this->w3 -= $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_4) {
            $this->w4 -= $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_5) {
            $this->w5 -= $money;
        }

        return $this->save();
    }

    /**
     * 转账操作
     * $me = 我的用户对象
     * $me_wallet 我的钱包对象
     * $accept = 接收用户对象
     * $accept_wallet = 接收用户的钱包对象
     * $wallet = 转账的钱包类型
     * $money = 转账金额
     */
    public static function transferMoney($me, $me_wallet, $accept, $accept_wallet, $wallet, $money)
    {

        if ($wallet == self::$WALLET_TYPE_1) {    //奖金币转账
            $me_wallet->w1 -= $money;
            $accept_wallet->w1 += $money;

        } elseif ($wallet == self::$WALLET_TYPE_2) {  //报单币转账
            $me_wallet->w2 -= $money;
            $accept_wallet->w2 += $money;

        } elseif ($wallet == self::$WALLET_TYPE_3) {    //购物币转账
            $me_wallet->w3 -= $money;
            $accept_wallet->w3 += $money;

        }

        return $me_wallet->save() &&
        $accept_wallet->save() &&
        MTransferLog::log($me, $accept, $wallet, $money);
    }

    /**
     * 钱包提现申请
     *
     * $wallet = 钱包类型
     * $money = 提现金额
     */
    public function withdraw($request_wallet, $request_money)
    {

        if ($request_wallet == self::$WALLET_TYPE_1) {  //奖金币提现
            $this->w1 -= $request_money;

        } elseif ($request_wallet == self::$WALLET_TYPE_2) {    //可用积分提现
            $this->w2 -= $request_money;
        } elseif ($request_wallet == self::$WALLET_TYPE_3) {    //可用积分提现
            $this->w3 -= $request_money;
        }
        return $this->save();
    }


    /**
     * 币种转换
     * $real_coin 原币种
     * $final_coin 目标币种
     * $money = 转换金额
     */
    public function coinSwitch($real_coin, $final_coin, $money)
    {
        if ($real_coin == self::$WALLET_TYPE_1) {
            $this->w1 -= $money;

        } elseif ($real_coin == self::$WALLET_TYPE_2) {
            $this->w2 -= $money;

        } elseif ($real_coin == self::$WALLET_TYPE_3) {
            $this->w3 -= $money;

        } elseif ($real_coin == self::$WALLET_TYPE_4) {
            $this->w4 -= $money;

        } elseif ($real_coin == self::$WALLET_TYPE_5) {
            $this->w5 -= $money;

        }

        if ($final_coin == self::$WALLET_TYPE_1) {
            $this->w1 += $money;

        } elseif ($final_coin == self::$WALLET_TYPE_2) {
            $this->w2 += $money;

        } elseif ($final_coin == self::$WALLET_TYPE_3) {
            $this->w3 += $money;

        } elseif ($final_coin == self::$WALLET_TYPE_4) {
            $this->w4 += $money;

        } elseif ($final_coin == self::$WALLET_TYPE_5) {
            $this->w5 += $money;

        }

        return $this->save();
    }

    /**
     * 会员转账数据验证
     */
    public static function transferValidate($flag = 0)
    {
        $rules = array(
            "accept_user" => "required",
            'wallet' => 'required|numeric',
            'money' => 'required|numeric|between:0,999999999',
            'safepass' => 'required|between:3,50',
        );

        $msg = array(
            "accept_user.required" => "必须填写接收人用户名！",
            "safepass.required" => "请填写交易密码！",
            "money.required" => "转账金额必须填写且为纯数字"
        );

        if ($flag == 0) {
            return $rules;
        } else {
            return $msg;
        }
    }

    /**
     * 钱包扣款函数
     * $wallet_type = 钱包类型
     * $money =购买商品的价格
     */
    public function buyShop($wallet_type, $money)
    {
        if ($wallet_type == self::$WALLET_TYPE_1) {  //奖金币充值
            $this->w1 -= $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_2) {  //报单币充值
            $this->w2 -= $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_3) {  //购物币充值
            $this->w3 -= $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_4) {
            $this->w4 -= $money;

        } elseif ($wallet_type == self::$WALLET_TYPE_5) {
            $this->w5 -= $money;
        }

        return $this->save();
    }

}